﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using com.pro2e.console.core.valueobject;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;

/// <summary>
/// ServiceImpl 的摘要描述
/// </summary>
/// 
namespace com.pro2e.console.service.core
{
    public class ServiceImpl
    {
        DaoImp dao = new DaoImp();

        public ServiceImpl()
        {

        }

        public T DaoGetVOById<T>(object keyId)
        {
            return dao.DaoGetVOById<T>(keyId);
        }

        public T DaoGetTmpVOById<T>(object keyId)
        {
            return dao.DaoGetTmpVOById<T>(keyId);
        }

        public void DaoUpdate(Object o)
        {
            dao.DaoUpdate(o);
        }
     
       
        public void DaoInsert(Object o)
        {
           
            dao.DaoInsert(o);
        }
       
        public void DaoSaveOrUpdate(Object o)
        {
            dao.DaoSaveOrUpdate(o);
        }
        public void DaoSaveOrUpdateCopy(Object o)
        {
            dao.DaoSaveOrUpdateCopy(o);
        }
        public void DaoDelete(Object o)
        {
            dao.DaoDelete(o);
        }
        

        /// <summary>
        /// 計算總筆數
        /// </summary>
        /// <typeparam name="T">傳回的物件</typeparam>
        /// <returns>筆數</returns>
        public int DaoTotalRecordCount<T>()
        {
            return (dao.DaoTotalRecordCount<T>());
        }


        /// <summary>
        /// 取出VO全部 by 分頁
        /// </summary>
        /// <typeparam name="T">類型</typeparam>
        /// <param name="firstRow">從那筆位置開始</param>
        /// <param name="MaxRecord">一頁幾筆</param>
        /// <returns></returns>
        public IList<T> DaoGetAllVOByPage<T>(int firstRow, int MaxRecord)
        {
            return dao.DaoGetAllVOByPage<T>(firstRow, MaxRecord);
        }

        /// <summary>
        /// 取出VO全部 by 分頁
        /// </summary>
        /// <typeparam name="T">類型</typeparam>
        /// <param name="firstRow">從那筆位置開始</param>
        /// <param name="MaxRecord">一頁幾筆</param>
        /// <param name="sortOrder">排序欄位</param>
        /// <returns></returns>
        public IList<T> DaoGetAllVOByPage<T>(int firstRow, int MaxRecord, Order sortOrder)
        {
            return dao.DaoGetAllVOByPage<T>(firstRow, MaxRecord, sortOrder);
        }

        /// <summary>
        /// 算出該VO的全部數量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public int SVGetVOCount<T>()
        {
            return (dao.DaoTotalRecordCount<T>());
        }


        public T GetVO<T>(object id)
        {
            return (dao.DaoGetVOById<T>(id));
        }


        /// <summary>
        /// 取得全部的VO
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public IList<T> DaoGetAllVO<T>()
        {
            return (dao.DaoGetAllVO<T>());
        }


        /// <summary>
        /// 執行預先定義好的DetachedCriteria
        /// </summary>
        /// <typeparam name="T">轉型型別</typeparam>
        /// <param name="detachedcriteria">預先準備好的條件</param>
        /// <param name="firstRow">第一筆的位置</param>
        /// <param name="MaxRecord">取最多幾筆</param>
        /// <returns></returns>
        public IList<T> ExecutableDetachedCriteria<T>(DetachedCriteria detachedcriteria, int firstRow, int MaxRecord)
        {
            return (dao.ExecutableDetachedCriteria<T>(detachedcriteria, firstRow, MaxRecord));
        }



        /// <summary>
        /// 執行預先定義好的DetachedCriteria並傳回一筆資料
        /// </summary>
        /// <typeparam name="T">轉型型別</typeparam>
        /// <param name="detachedcriteria">預先準備好的條件</param>
        /// <param name="firstRow">第一筆的位置</param>
        /// <param name="MaxRecord">取最多幾筆</param>
        /// <returns></returns>
        public T ExecutableDetachedCriteriaUnique<T>(DetachedCriteria detachedcriteria)
        {
            return (dao.ExecutableDetachedCriteriaUnique<T>(detachedcriteria));
        }

        /// <summary>
        /// 計算總筆數
        /// </summary>
        /// <returns></returns>
        public int CountDetachedCriteriaRow(DetachedCriteria detachedcriteria)
        {
            return dao.CountDetachedCriteriaRow(detachedcriteria);
        }

        /// <summary>
        /// 執行預先定義好的DetachedCriteria
        /// </summary>
        /// <typeparam name="T">轉型型別</typeparam>
        /// <returns></returns>
        public IList<T> ExecutableDetachedCriteria<T>(DetachedCriteria detachedcriteria)
        {
            return (dao.ExecutableDetachedCriteria<T>(detachedcriteria));
        }


        /// <summary>
        /// 動態查詢
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<T> DaoGetByWhere<T>(string where)
        {
            return (dao.DaoGetByWhere<T>(where));
        }


        /// <summary>
        /// 動態查詢並分頁結果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <param name="starIndex">首筆位置</param>
        /// <param name="maxRecord">最大筆數</param>
        /// <returns></returns>
        public IList<T> DaoGetByWhere<T>(string where, int starIndex, int maxRecord)
        {
            return (dao.DaoGetByWhere<T>(where, starIndex, maxRecord));
        }

        /// <summary>
        /// 動態查詢筆數
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public int DaoCountByWhere<T>(string where)
        {
            return dao.DaoCountByWhere<T>(where);
        }

        /// <summary>
        /// 執行預先定義好的DetachedCriteria 並能進行排序
        /// </summary>
        /// <typeparam name="T">轉型型別</typeparam>
        /// <param name="detachedcriteria">預先準備好的條件</param>
        /// <param name="firstRow">第一筆的位置</param>
        /// <param name="MaxRecord">取最多幾筆</param>
        /// <param name="sortExpression">排序欄位</param>
        /// <param name="isAsc">是否為遞增asc排序</param>
        /// <returns></returns>
        public IList<T> ExecutableDetachedCriteria<T>(DetachedCriteria detachedcriteria, int firstRow, int MaxRecord, string sortExpression, bool isAsc)
        {
            if (string.IsNullOrEmpty(sortExpression))
            {
                return (dao.ExecutableDetachedCriteria<T>(detachedcriteria, firstRow, MaxRecord));
            }
            else
            {
                int isSubSort = sortExpression.IndexOf(".");
                detachedcriteria.Orders.Clear();

                //sub query
                if (isSubSort != -1)
                {
                    string sortObject = sortExpression.Substring(0, isSubSort);
                    string sortFiled = sortExpression.Substring(isSubSort + 1);
                    if (detachedcriteria.SubcriteriaList.Count > 0)
                    {
                        NHibernate.Impl.CriteriaImpl.Subcriteria tmpCri = null;
                        foreach (NHibernate.Impl.CriteriaImpl.Subcriteria subCri in detachedcriteria.SubcriteriaList)
                        {
                            if (subCri.Path.Equals(sortObject))
                            {
                                tmpCri = subCri;
                                break;
                            }
                        }

                        if (tmpCri == null)
                        {
                            DetachedCriteria subDetatch = detachedcriteria.CreateCriteria(sortObject);
                            DetachedcriteriaAddOrder(subDetatch, sortFiled, isAsc);
                        }
                        else
                        {
                            if (isAsc)
                            {
                                tmpCri.AddOrder(Order.Asc(sortFiled));
                            }
                            else
                            {
                                tmpCri.AddOrder(Order.Desc(sortFiled));
                            }
                        }
                    }
                    else
                    {
                        DetachedCriteria subDetatch = detachedcriteria.CreateCriteria(sortObject);
                        DetachedcriteriaAddOrder(subDetatch, sortFiled, isAsc);
                    }
                }
                else
                {
                    DetachedcriteriaAddOrder(detachedcriteria, sortExpression, isAsc);
                }

                return (dao.ExecutableDetachedCriteria<T>(detachedcriteria, firstRow, MaxRecord));
            }
        }

    

        private void DetachedcriteriaAddOrder(DetachedCriteria detachedcriteria, string sortExpression, bool isAsc)
        {
            if (isAsc)
            {
                detachedcriteria.AddOrder(Order.Asc(sortExpression));
            }
            else
            {
                detachedcriteria.AddOrder(Order.Desc(sortExpression));
            }
        }


    }
}